package com.latinia.limsp.test;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map.Entry;
import javax.jms.JMSException;
import javax.naming.NamingException;
import javax.xml.rpc.ServiceException;
import com.latinia.limsp.db.MsgPersistence;
import com.latinia.limsp.util.message.xml.ContentFormatter;
import com.latinia.limsp.util.message.xml.MessageFormatterException;
import com.latinia.limsp.util.message.xml.MessageFormatterFactory;
import com.latinia.limsp.util.message.xml.MessageMtFormatter;
import com.latinia.limsp.util.message.xml.TemplateParamsFormatter;
/***	
 * Clase que gestiona el envio de SMS
 * @author Latinia OTC.
 **/
public class SendSMS extends SendMessage {
	String xmlFormatter;
	MessageMtFormatter mtFormatter;
	XMLManager xmlManager;
	private Contract contract;
	Destination destination;	
	    public SendSMS(XMLManager manager,Contract contract) throws SQLException, NamingException{
	    	super(contract.getProperties());
	    	xmlManager=manager;
	    	this.contract=contract;
	    }	
	    
	/**
	 *  
	 * @throws JMSException 
	 * @throws SQLException 
	 * @throws NamingException 
	 * @
	 * */
	 public void sendMessageJMS(int i, MsgPersistence msgPersistence)throws JMSException, SQLException, NamingException,Exception{		 
		    mtFormatter.setMessageOut(destination.getMessage()+" JMS "+i);			  
		 	sendByJMS(mtFormatter.toString());
		 	msgPersistence.insert(contract.getTest_case(), mtFormatter.getID(),mtFormatter.toString());
	        System.out.println("SMS "+i+" enviado por JMS");	
	 }
	 public void sendMessageWS(int i, MsgPersistence msgPersistence)throws MalformedURLException,ServiceException,RemoteException,Exception{	     	 
            mtFormatter.setMessageOut(destination.getMessage()+" WS "+i);          
            sendByWS(mtFormatter.toString());
            System.out.println("SMS "+i+" enviado por WS");         
            msgPersistence.insert(contract.getTest_case(), mtFormatter.getID(),mtFormatter.toString());
	    }
	 public void createXML() throws MessageFormatterException{
		 
	            mtFormatter = MessageFormatterFactory.createMessageMt(MessageFormatterFactory.createUniqueID(new Object()), "sms", "text");	 
	            destination=contract.getDestination();
	            if(destination==null)
	        		throw new RuntimeException("No se encontro un destino con codigo \""+contract.getDestination()+"\"");
	            mtFormatter.setGsmDest(destination.getTo());	        	            	            	     
	            if (contract.getId()!=null) mtFormatter.setIdContract(contract.getId());
	            else if (contract.getRef() != null) {
	                mtFormatter.setLoginEnterprise(contract.getCompany());
	                mtFormatter.setRefContract(contract.getRef());
	            }
	            String refTemplate=contract.getTemplate();	      
//	            mtFormatter.setInfoElem("billable", "true");    
	            if(refTemplate!=null)
	            {
	            	if(!refTemplate.equals("")){
	            		mtFormatter.setMessageOut("Test Template");
	            		ContentFormatter content=(ContentFormatter)mtFormatter.getAllContents().next();	            		
						if(xmlManager.getTemplates().get(refTemplate)!=null)
						{
							content.setRefTemplate(refTemplate);
		            		content.setRefTemplateParams(refTemplate);
							Iterator<Entry<String, String>> i=xmlManager.getTemplates().get(refTemplate).entrySet().iterator();
							TemplateParamsFormatter templateParams=mtFormatter.addTemplateParams(refTemplate);
							while(i.hasNext()){					
								Entry<String, String> e=(Entry<String, String>)i.next();
								String key=(String)e.getKey(),value=(String)e.getValue();	            		            		
								templateParams.setParamValue(key,value);
							}
						}
	            	}	            							            
	            }
	            if(destination.getLabel()!=null)
	            	mtFormatter.setRefMsgLabel(destination.getLabel());
	            if(destination.getTsDelivery()!=null)
	            	mtFormatter.setTimestampDelivery(destination.getTsDelivery());
	            if(destination.getTsDeliveryEnd()!=null)
	            	mtFormatter.setTimestampDeliveryEnd(destination.getTsDeliveryEnd());
	            if(destination.getTsExpire()!=null)
	            	mtFormatter.setTimestampExpire(destination.getTsExpire());	            
	 }
}